{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/pratikbhavsar/miniconda3/envs/langgraph/lib/python3.12/site-packages/pydantic/_internal/_config.py:341: UserWarning: Valid config keys have changed in V2:\n",
      "* 'fields' has been removed\n",
      "  warnings.warn(message, UserWarning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "import random\n",
    "random.seed(42)\n",
    "from pprint import pprint\n",
    "\n",
    "import pandas as pd\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "import promptquality as pq\n",
    "from tau_bench.envs.retail.tasks import tasks as retail_tasks\n",
    "from tau_bench.envs.retail.tools import ALL_TOOLS as retail_tools\n",
    "from tau_bench.envs.airline.tasks import tasks as airline_tasks\n",
    "from tau_bench.envs.airline.tools import ALL_TOOLS as airline_tools\n",
    "from tqdm import tqdm\n",
    "tqdm.pandas()\n",
    "\n",
    "load_dotenv()\n",
    "# pq.login(\"console.demo.rungalileo.io\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "retail_instructions = [task[\"instruction\"] for task in retail_tasks]\n",
    "airline_instructions = [task[\"instruction\"] for task in airline_tasks]\n",
    "\n",
    "#select 50 random instructions for each domain\n",
    "retail_instructions = random.sample(retail_instructions, 50)\n",
    "airline_instructions = random.sample(airline_instructions, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert_tau_to_langchain_format(openai_tool):\n",
    "    \"\"\"Convert OpenAI function format to LangChain format\"\"\"\n",
    "    \n",
    "    def convert_property(prop_dict):\n",
    "        \"\"\"Convert property while preserving all attributes\"\"\"\n",
    "        converted = {\n",
    "            \"type\": prop_dict[\"type\"],\n",
    "            \"description\": prop_dict.get(\"description\", \"\"),\n",
    "            \"title\": prop_dict.get(\"title\", \"\")\n",
    "        }\n",
    "        \n",
    "        # Handle enums\n",
    "        if \"enum\" in prop_dict:\n",
    "            converted[\"enum\"] = prop_dict[\"enum\"]\n",
    "            \n",
    "        # Handle array items\n",
    "        if prop_dict[\"type\"] == \"array\" and \"items\" in prop_dict:\n",
    "            items = prop_dict[\"items\"]\n",
    "            if items[\"type\"] == \"object\":\n",
    "                converted[\"items\"] = {\n",
    "                    \"type\": \"object\",\n",
    "                    \"properties\": {\n",
    "                        k: convert_property(v) \n",
    "                        for k, v in items[\"properties\"].items()\n",
    "                    }\n",
    "                }\n",
    "                if \"required\" in items:\n",
    "                    converted[\"items\"][\"required\"] = items[\"required\"]\n",
    "            else:\n",
    "                converted[\"items\"] = {\"type\": items[\"type\"]}\n",
    "                \n",
    "        return converted\n",
    "\n",
    "    function_data = openai_tool[\"function\"]\n",
    "    \n",
    "    converted = [{\n",
    "        \"description\": function_data[\"description\"],\n",
    "        \"properties\": {\n",
    "            k: convert_property(v)\n",
    "            for k, v in function_data[\"parameters\"][\"properties\"].items()\n",
    "        },\n",
    "        \"required\": function_data[\"parameters\"][\"required\"],\n",
    "        \"title\": function_data[\"name\"],\n",
    "        \"type\": \"object\"\n",
    "    }]\n",
    "    \n",
    "    return converted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>conversation</th>\n",
       "      <th>tools_langchain</th>\n",
       "      <th>n_turns</th>\n",
       "      <th>len_query</th>\n",
       "      <th>n_tools</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>[{'role': 'user', 'content': 'You name is Jame...</td>\n",
       "      <td>[{'description': 'Calculate the result of a ma...</td>\n",
       "      <td>1</td>\n",
       "      <td>306</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[{'role': 'user', 'content': 'You are mia_garc...</td>\n",
       "      <td>[{'description': 'Calculate the result of a ma...</td>\n",
       "      <td>1</td>\n",
       "      <td>302</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[{'role': 'user', 'content': 'You are Yusuf Ro...</td>\n",
       "      <td>[{'description': 'Calculate the result of a ma...</td>\n",
       "      <td>1</td>\n",
       "      <td>296</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[{'role': 'user', 'content': 'You name is Lei ...</td>\n",
       "      <td>[{'description': 'Calculate the result of a ma...</td>\n",
       "      <td>1</td>\n",
       "      <td>239</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[{'role': 'user', 'content': 'You are aarav_sa...</td>\n",
       "      <td>[{'description': 'Calculate the result of a ma...</td>\n",
       "      <td>1</td>\n",
       "      <td>435</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        conversation  \\\n",
       "0  [{'role': 'user', 'content': 'You name is Jame...   \n",
       "1  [{'role': 'user', 'content': 'You are mia_garc...   \n",
       "2  [{'role': 'user', 'content': 'You are Yusuf Ro...   \n",
       "3  [{'role': 'user', 'content': 'You name is Lei ...   \n",
       "4  [{'role': 'user', 'content': 'You are aarav_sa...   \n",
       "\n",
       "                                     tools_langchain  n_turns  len_query  \\\n",
       "0  [{'description': 'Calculate the result of a ma...        1        306   \n",
       "1  [{'description': 'Calculate the result of a ma...        1        302   \n",
       "2  [{'description': 'Calculate the result of a ma...        1        296   \n",
       "3  [{'description': 'Calculate the result of a ma...        1        239   \n",
       "4  [{'description': 'Calculate the result of a ma...        1        435   \n",
       "\n",
       "   n_tools  \n",
       "0       16  \n",
       "1       16  \n",
       "2       16  \n",
       "3       16  \n",
       "4       16  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "retail_langchain_tools = [convert_tau_to_langchain_format(tool.get_info())[0] for tool in retail_tools]\n",
    "airline_langchain_tools = [convert_tau_to_langchain_format(tool.get_info())[0] for tool in airline_tools]\n",
    "\n",
    "conversations = [[{\"role\": \"user\", \"content\": instruction}] for instruction in retail_instructions + airline_instructions]\n",
    "tools = [retail_langchain_tools]*len(retail_instructions) + [airline_langchain_tools]*len(airline_instructions)\n",
    "\n",
    "df = pd.DataFrame({\"conversation\": conversations, \"tools_langchain\": tools})\n",
    "df[\"n_turns\"] = df.conversation.apply(lambda x: len([m for m in x if m[\"role\"] == \"user\"]))\n",
    "df[\"len_query\"] = df.conversation.apply(lambda x: len(x[-1][\"content\"]))\n",
    "df[\"n_tools\"] = df.tools_langchain.apply(lambda x: len(x))\n",
    "df.to_parquet(\"../data/datasets/tau_long_context.parquet\", engine=\"fastparquet\")\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langgraph",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
